package me.raih.service.impl;

import me.raih.mapper.SalesMapper;
import me.raih.pojo.po.Sales;
import me.raih.service.SalesService;
import me.raih.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Raih
 * @Time 2020/12/29 12:33
 */
@Service
public class SalesServiceImpl implements SalesService {

    @Autowired
    SalesMapper salesMapper;

    @Override
    public int updateSales(Sales sales) {
        int i = salesMapper.updateSales(sales);
        return i;
    }

    @Override
    public int findSalesNumber() {
        int i = salesMapper.findSalesNumber();
        return Math.max(0,i);
    }

    @Override
    public int findTodaySalesNumber() {
        int i = salesMapper.findTodaySalesNumber();
        return Math.max(0,i);
    }

    @Override
    public int findYesterdaySalesNumber() {
        int i = salesMapper.findYesterdaySalesNumber();
        return Math.max(0,i);
    }

    @Override
    public int findCurrMonthSalesNumber() {
        int i = salesMapper.findCurrMonthSalesNumber();
        return Math.max(0,i);
    }

    @Override
    public int findCurrWeekSalesNumber() {
        int i = salesMapper.findCurrWeekSalesNumber();
        return Math.max(0,i);
    }

    @Override
    public Sales login(String username, String password) {

        Sales sakesClerk = salesMapper.findSalesByUsernameAndPassword(username, password);

        return sakesClerk;
    }

    @Override
    public List<Sales> findAllSales() {

        List<Sales> sales = salesMapper.findAllSales();

        return sales;
    }

    @Override
    public Sales findSalesBySalesName(String salesName) {

        return salesMapper.findSalesByUsername(salesName);
    }

    @Override
    public boolean addSales() {

        Sales sales = new Sales();
        int no = salesMapper.needYourNo()+1;
        sales.setName("陌生售货员");
        sales.setPower(1);
        sales.setCreateTime(new Date());
        sales.setUsername("sales"+no);
        sales.setPassword(UUIDUtil.bigString().substring(3,10));
        int i = salesMapper.insertSales(sales);
        return i == 1;
    }

    @Override
    public boolean deleteSalesByUsername(String username) {
        int i = salesMapper.deleteSales(username);
        return i == 1;
    }
}
